<template>
  <el-card class="box-card loading-area">
    <div slot="header" class="clearfix">
      <span>生成license证书</span>
    </div>
    <div style="align-content: center">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="授权IP" prop="licenseIp">
          <el-input v-model="formData.licenseIp" placeholder="请输入要授权IP地址" clearable :style="{width: '100%'}"/>
        </el-form-item>
        <el-form-item label="license模式" prop="mode">
          <el-radio-group v-model="formData.mode">
            <el-radio
              v-for="dict in modeOptions"
              :key="dict.dictCode"
              :label="dict.dictCode"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="授权有效期" prop="expiryTime">
          <el-date-picker type="datetime" v-model="formData.expiryTime" format="yyyy-MM-dd HH:mm:ss"
                          value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" placeholder="请选择授权有效期" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item size="medium">
          <el-button type="primary" @click="submitForm">生成证书</el-button>
          <el-button @click="resetForm">重置</el-button>
          <el-button
            v-if="licenseFileUrl"
            type="primary"
            icon="el-icon-download"
            @click="handleDownload(licenseFileUrl)"
          >立即下载</el-button>
        </el-form-item>
      </el-form>

    </div>
  </el-card>

</template>
<script>
  import {createAndDownload} from "@/api/comm/license";
  import { download } from "@/utils/commonUtil";
  export default {
    name: "license",
    data() {
      return {
        qrCodeUrl:undefined,
        //授权模式数据字典
        modeOptions: [],
        licenseFileUrl:undefined,
        formData: {
          licenseIp: undefined,
          mode: 0,
          expiryTime: undefined
        },
        rules: {
          licenseIp: [
            {required: true, message: "请输入授权IP", trigger: "blur"}
          ],
          mode: [
            {required: true, message: "请选择license模式", trigger: "blur"}
          ],
          expiryTime: [
            {required: true, message: "请选择授权有效期", trigger: "blur"}
          ]
        }
      }
    },
    created() {
      this.getDicts("licenseMode").then(res => {
        this.modeOptions = [];
        res.data.forEach((dict)=>{
          let tmpDict = {};
          tmpDict.dictCode = Number(dict.dictCode);//数据类型转换，el-select数据回显数据类型必须要一致
          tmpDict.dictLabel = dict.dictLabel;
          this.modeOptions.push(tmpDict);
        });
      });
    },
    methods: {
      submitForm() {
        this.$refs['elForm'].validate(valid => {
          if (!valid) return
          createAndDownload(this.formData).then(response => {
            if (response.code === 0) {
              this.msgSuccess("生成成功");
              this.licenseFileUrl = response.data;
            } else {
              this.msgError(response.msg);
            }
          });
        })
      },
      /** 下载按钮操作 */
      handleDownload(fileUrl) {
        const fileName = 'appLicense.lic';
        download(fileUrl,fileName)
      },
      resetForm() {
        this.$refs['elForm'].resetFields()
      }
    }
  }
</script>
<style lang="scss" scoped>
  .box-card {
    margin: 10px 20%;
  }
</style>
